﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace HospitalManagement.DAL
{
	/// <summary>
	/// 数据访问类:PatientInfo
	/// </summary>
	public partial class PatientInfo
	{
		public PatientInfo()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("SysNo", "PatientInfo"); 
		}


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from PatientInfo");
			strSql.Append(" where SysNo="+SysNo+" ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HospitalManagement.Model.PatientInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.FullName != null)
			{
				strSql1.Append("FullName,");
				strSql2.Append("'"+model.FullName+"',");
			}
			if (model.AnonymousName != null)
			{
				strSql1.Append("AnonymousName,");
				strSql2.Append("'"+model.AnonymousName+"',");
			}
			if (model.Birthday != null)
			{
				strSql1.Append("Birthday,");
				strSql2.Append("'"+model.Birthday+"',");
			}
			if (model.BirthPlace != null)
			{
				strSql1.Append("BirthPlace,");
				strSql2.Append("'"+model.BirthPlace+"',");
			}
			if (model.Address != null)
			{
				strSql1.Append("Address,");
				strSql2.Append("'"+model.Address+"',");
			}
			if (model.Mobile != null)
			{
				strSql1.Append("Mobile,");
				strSql2.Append("'"+model.Mobile+"',");
			}
			if (model.Phone != null)
			{
				strSql1.Append("Phone,");
				strSql2.Append("'"+model.Phone+"',");
			}
			if (model.Email != null)
			{
				strSql1.Append("Email,");
				strSql2.Append("'"+model.Email+"',");
			}
			if (model.Contact != null)
			{
				strSql1.Append("Contact,");
				strSql2.Append("'"+model.Contact+"',");
			}
			if (model.Relation != null)
			{
				strSql1.Append("Relation,");
				strSql2.Append("'"+model.Relation+"',");
			}
			if (model.Survival != null)
			{
				strSql1.Append("Survival,");
				strSql2.Append("'"+model.Survival+"',");
			}
			if (model.Sick != null)
			{
				strSql1.Append("Sick,");
				strSql2.Append("'"+model.Sick+"',");
			}
			if (model.progressionTime != null)
			{
				strSql1.Append("progressionTime,");
				strSql2.Append("'"+model.progressionTime+"',");
			}
			if (model.ComeInTime != null)
			{
				strSql1.Append("ComeInTime,");
				strSql2.Append("'"+model.ComeInTime+"',");
			}
			if (model.Disabled != null)
			{
				strSql1.Append("Disabled,");
				strSql2.Append(""+model.Disabled+",");
			}
			if (model.Indate != null)
			{
				strSql1.Append("Indate,");
				strSql2.Append("'"+model.Indate+"',");
			}
			if (model.InUser != null)
			{
				strSql1.Append("InUser,");
				strSql2.Append("'"+model.InUser+"',");
			}
            if (model.EditDate != null)
            {
                strSql1.Append("EditDate,");
                strSql2.Append("'" + model.EditDate + "',");
            }
            if (model.EditUser != null)
            {
                strSql1.Append("EditUser,");
                strSql2.Append("'" + model.EditUser + "',");
            }
			strSql.Append("insert into PatientInfo(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			strSql.Append(";select @@IDENTITY");
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HospitalManagement.Model.PatientInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update PatientInfo set ");
			if (model.FullName != null)
			{
				strSql.Append("FullName='"+model.FullName+"',");
			}
			else
			{
				strSql.Append("FullName= null ,");
			}
			if (model.AnonymousName != null)
			{
				strSql.Append("AnonymousName='"+model.AnonymousName+"',");
			}
			else
			{
				strSql.Append("AnonymousName= null ,");
			}
			if (model.Birthday != null)
			{
				strSql.Append("Birthday='"+model.Birthday+"',");
			}
			else
			{
				strSql.Append("Birthday= null ,");
			}
			if (model.BirthPlace != null)
			{
				strSql.Append("BirthPlace='"+model.BirthPlace+"',");
			}
			else
			{
				strSql.Append("BirthPlace= null ,");
			}
			if (model.Address != null)
			{
				strSql.Append("Address='"+model.Address+"',");
			}
			else
			{
				strSql.Append("Address= null ,");
			}
			if (model.Mobile != null)
			{
				strSql.Append("Mobile='"+model.Mobile+"',");
			}
			else
			{
				strSql.Append("Mobile= null ,");
			}
			if (model.Phone != null)
			{
				strSql.Append("Phone='"+model.Phone+"',");
			}
			else
			{
				strSql.Append("Phone= null ,");
			}
			if (model.Email != null)
			{
				strSql.Append("Email='"+model.Email+"',");
			}
			else
			{
				strSql.Append("Email= null ,");
			}
			if (model.Contact != null)
			{
				strSql.Append("Contact='"+model.Contact+"',");
			}
			else
			{
				strSql.Append("Contact= null ,");
			}
			if (model.Relation != null)
			{
				strSql.Append("Relation='"+model.Relation+"',");
			}
			else
			{
				strSql.Append("Relation= null ,");
			}
			if (model.Survival != null)
			{
				strSql.Append("Survival='"+model.Survival+"',");
			}
			else
			{
				strSql.Append("Survival= null ,");
			}
			if (model.Sick != null)
			{
				strSql.Append("Sick='"+model.Sick+"',");
			}
			else
			{
				strSql.Append("Sick= null ,");
			}
			if (model.progressionTime != null)
			{
				strSql.Append("progressionTime='"+model.progressionTime+"',");
			}
			else
			{
				strSql.Append("progressionTime= null ,");
			}
			if (model.ComeInTime != null)
			{
				strSql.Append("ComeInTime='"+model.ComeInTime+"',");
			}
			else
			{
				strSql.Append("ComeInTime= null ,");
			}
			if (model.Disabled != null)
			{
				strSql.Append("Disabled="+model.Disabled+",");
			}
			else
			{
				strSql.Append("Disabled= null ,");
			}
			if (model.Indate != null)
			{
				strSql.Append("Indate='"+model.Indate+"',");
			}
			else
			{
				strSql.Append("Indate= null ,");
			}
			if (model.InUser != null)
			{
				strSql.Append("InUser='"+model.InUser+"',");
			}
			else
			{
				strSql.Append("InUser= null ,");
			}
			if (model.EditDate != null)
			{
				strSql.Append("EditDate='"+model.EditDate+"',");
			}
			else
			{
				strSql.Append("EditDate= null ,");
			}
			if (model.EditUser != null)
			{
				strSql.Append("EditUser='"+model.EditUser+"',");
			}
			else
			{
				strSql.Append("EditUser= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where SysNo="+ model.SysNo+"");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PatientInfo ");
			strSql.Append(" where SysNo="+SysNo+"" );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string SysNolist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from PatientInfo ");
			strSql.Append(" where SysNo in ("+SysNolist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HospitalManagement.Model.PatientInfo GetModel(int SysNo)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" SysNo,FullName,AnonymousName,Birthday,BirthPlace,Address,Mobile,Phone,Email,Contact,Relation,Survival,Sick,progressionTime,ComeInTime,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" from PatientInfo ");
			strSql.Append(" where SysNo="+SysNo+"" );
			HospitalManagement.Model.PatientInfo model=new HospitalManagement.Model.PatientInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HospitalManagement.Model.PatientInfo DataRowToModel(DataRow row)
		{
			HospitalManagement.Model.PatientInfo model=new HospitalManagement.Model.PatientInfo();
			if (row != null)
			{
				if(row["SysNo"]!=null && row["SysNo"].ToString()!="")
				{
					model.SysNo=int.Parse(row["SysNo"].ToString());
				}
				if(row["FullName"]!=null)
				{
					model.FullName=row["FullName"].ToString();
				}
				if(row["AnonymousName"]!=null)
				{
					model.AnonymousName=row["AnonymousName"].ToString();
				}
				if(row["Birthday"]!=null && row["Birthday"].ToString()!="")
				{
					model.Birthday=DateTime.Parse(row["Birthday"].ToString());
				}
				if(row["BirthPlace"]!=null)
				{
					model.BirthPlace=row["BirthPlace"].ToString();
				}
				if(row["Address"]!=null)
				{
					model.Address=row["Address"].ToString();
				}
				if(row["Mobile"]!=null)
				{
					model.Mobile=row["Mobile"].ToString();
				}
				if(row["Phone"]!=null)
				{
					model.Phone=row["Phone"].ToString();
				}
				if(row["Email"]!=null)
				{
					model.Email=row["Email"].ToString();
				}
				if(row["Contact"]!=null)
				{
					model.Contact=row["Contact"].ToString();
				}
				if(row["Relation"]!=null)
				{
					model.Relation=row["Relation"].ToString();
				}
				if(row["Survival"]!=null)
				{
					model.Survival=row["Survival"].ToString();
				}
				if(row["Sick"]!=null)
				{
					model.Sick=row["Sick"].ToString();
				}
				if(row["progressionTime"]!=null)
				{
					model.progressionTime=row["progressionTime"].ToString();
				}
				if(row["ComeInTime"]!=null && row["ComeInTime"].ToString()!="")
				{
					model.ComeInTime=DateTime.Parse(row["ComeInTime"].ToString());
				}
				if(row["Disabled"]!=null && row["Disabled"].ToString()!="")
				{
					model.Disabled=int.Parse(row["Disabled"].ToString());
				}
				if(row["Indate"]!=null && row["Indate"].ToString()!="")
				{
					model.Indate=DateTime.Parse(row["Indate"].ToString());
				}
				if(row["InUser"]!=null)
				{
					model.InUser=row["InUser"].ToString();
				}
				if(row["EditDate"]!=null && row["EditDate"].ToString()!="")
				{
					model.EditDate=DateTime.Parse(row["EditDate"].ToString());
				}
				if(row["EditUser"]!=null)
				{
					model.EditUser=row["EditUser"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SysNo,FullName,AnonymousName,Birthday,BirthPlace,Address,Mobile,Phone,Email,Contact,Relation,Survival,Sick,progressionTime,ComeInTime,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" FROM PatientInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" SysNo,FullName,AnonymousName,Birthday,BirthPlace,Address,Mobile,Phone,Email,Contact,Relation,Survival,Sick,progressionTime,ComeInTime,Disabled,Indate,InUser,EditDate,EditUser ");
			strSql.Append(" FROM PatientInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM PatientInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.SysNo desc");
			}
			strSql.Append(")AS Row, T.*  from PatientInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

